<?php
include("../../includes/db_connect.php");
include("../../includes/safefunctions.php");
session_start();
	
if(isset($_POST["submit"])) {
	echo "<div id=\"result\">";
	$name = safety($_POST["name"]);
	$year = safety($_POST["year"]);
	$start = safety($_POST["start"]);
	$end = safety($_POST["end"]);
	$classId = safety($_POST["classId"]);
	$courseName = "$name - $year";
	$classQuery = mysqli_query($db, "SELECT name, year FROM class WHERE id='$classId' LIMIT 1");
	if(mysqli_num_rows($classQuery) === 1) {
		$classQuery = $classQuery->fetch_assoc();
		$className = $classQuery["name"];
		$classYear = $classQuery["year"];
		$classQuery = mysqli_query($db, "INSERT INTO courses (name, classId, start, end) VALUES ('$courseName', '$classId', '$start', '$end')");
		if($classQuery) {
			$pageIdQuery = mysqli_query($db, "SELECT id FROM pages WHERE name='$className - $classYear'");
			$pageId = $pageIdQuery->fetch_assoc()["id"];
			$pageQuery = mysqli_query($db, "INSERT INTO pages (name, tagName, parentId, hidden, script, text, file, allowScript, isLink) VALUES ('$courseName','$name','$pageId','0','','', NULL,'0','0')");
			echo "Skapade ny kurs";
		}else{
			echo "Kunde inte lägga in en ny kurs";
		}
	}else{
		echo "Kunde inte lägga in en ny kurs";
	}
	
	echo "</div>";
}else{
	if(isset($_SESSION["classId"]) && $_SESSION["classId"] > 0)
		$classQuery = mysqli_query($db, "SELECT id, name, year FROM class WHERE id='{$_SESSION["classId"]}'");
	else
		$classQuery = mysqli_query($db, "SELECT id, name, year FROM class");
	echo "<div id=\"result\"></div>";
	echo "<form class=\"formEducation\" action=\"\" method=\"POST\">
	<table>
	<tr><td>NAMN:</td><td>
	<input type=\"text\" name=\"name\" maxlength=\"50\"></td>
	</tr><tr><td>ÅR:</td><td>
	<input type=\"number\" name=\"year\"maxlength=\"4\"></td>
	</tr><tr><td>START:</td><td>
	<input type=\"date\" name=\"start\" maxlength=\"10\"></td>
	</tr><tr><td>SLUT:</td><td>
	<input type=\"date\" name=\"end\" maxlength=\"10\"></td></tr><tr><td>";
	echo "UTBILDNIG:</td><td>";
	echo "<select method=\"POST\" form=\"formEducation\" name=\"classId\">";
	while($class = $classQuery->fetch_assoc()) {
		echo "<option value=\"{$class["id"]}\">{$class["name"]} - {$class["year"]}</option>";
	}
	echo "</select>";
	echo "</td></tr><tr><td><input type=\"submit\"name=\"submit\" value=\"Hämta\"></td></tr>
	</table>
	</form>";
}
?>

<script src="../script/jquery-1.11.1.min.js"></script>
<script src="../script/ajaxlinks.js"></script>
<script>
var _SESSION = {};
_SESSION["currentPage"] = "createCourse.php";
$('form.formEducation').submit(function (e) {
	e.preventDefault();
	var form = document.forms[0];
	var url = "submit=get";
	url += "&name="+form["name"].value;
	url += "&year="+form["year"].value;
	url += "&start="+form["start"].value;
	url += "&end="+form["end"].value;
	url += "&classId="+form["classId"].value;
	
	var page = getPage(_SESSION["currentPage"], "POST", url);
	var tmpDiv = document.createElement("div");
	tmpDiv.innerHTML = page;
	document.getElementById("result").innerHTML = tmpDiv.querySelector("[id=result]").innerHTML;
	
	return false;

});
</script>